

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;


public class Horloge {
	
	
	private int iSecUnit, iSecDix, iMinUnit, iMinDix, iHeure;
	private int iTime = 1000;
	private Display myDisplay;
	Runnable timer;
	private Shell myShell;
	private Label lChrono;
	private String sTemps;
	

	public Horloge(Shell shell) {
	
		
		myShell = shell;
		myDisplay = myShell.getDisplay();
		init();
		lChrono = new Label(shell, SWT.NONE);
		lChrono.setBounds(350, 0, 148, 20);
		lChrono.setText(String.valueOf(iHeure)+ ":" +String.valueOf(iMinDix)+ String.valueOf(iMinUnit)+":"+String.valueOf(iSecDix)+String.valueOf(iSecUnit) );
		
		timer = new Runnable() {
			public void run() {
				iSecUnit++;
				
				if(iSecUnit == 10)
				{
					iSecDix++;
					iSecUnit = 0;
				}
				if (iSecDix == 7)
				{
					iMinUnit++;
					iSecDix = 0;
				}
				
				if (iMinUnit == 10)
				{
					iMinDix++;
					iMinUnit = 0;
				}
				
				if (iMinDix == 7)
				{
					iHeure++;
				}
				
				lChrono.setText(String.valueOf(iHeure)+ ":" +String.valueOf(iMinDix)+ String.valueOf(iMinUnit)+":"+String.valueOf(iSecDix)+String.valueOf(iSecUnit) );
				myDisplay.readAndDispatch();
				myDisplay.timerExec (iTime, this);
			}
			
		};
		
	
	}
	
	public void etatChrono( boolean bStart) {
		if (bStart)
			startChrono();
		else
			stopChrono();
	}
	
	public void init() {
		iSecDix = 0;
		iSecUnit =0;
		iMinDix =0;
		iMinUnit =0;
		iHeure =0;
		
		if(lChrono != null)
			lChrono.setText(String.valueOf(iHeure)+ ":" +String.valueOf(iMinDix)+ String.valueOf(iMinUnit)+":"+String.valueOf(iSecDix)+String.valueOf(iSecUnit) );
	}
	
	private void startChrono() {
		myDisplay.timerExec (iTime, timer);
	}

	private void stopChrono() {
		sTemps = lChrono.getText();
		myDisplay.timerExec(-1, timer);
		
	}

	public String getsTemps() {
		return sTemps;
	}

	public void setsTemps(String sTemps) {
		this.sTemps = sTemps;
	}

}
